/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 */

import { ApiVersion } from '../const/sdk-const.js';
import { DistroFilterDimensionEnum } from '../enum/distro-filter-dimension-enum.js';
import { CoreModuleModelImpl } from '../model/module/core-module-model-impl.js';

import { DistroFilterConfig, DistroFilterHandler } from './distro-filter-handler.js';

/**
 * apiVersion对象校验
 * policy:include/exclude
 * value:[3,30]
 */
export class ApiVersionHandler extends DistroFilterHandler {
  validateDistroFilterRequest(sourceDistroFilterConfig: DistroFilterConfig, targetDistroFilterConfig: DistroFilterConfig) {
    this.doRequest(sourceDistroFilterConfig, targetDistroFilterConfig, DistroFilterDimensionEnum.API_VERSION);
  }

  getDimensionValue(distroFilterConfig: DistroFilterConfig): number[] | undefined {
    return this.getCommonDimensionValue(
      distroFilterConfig,
      this.getApiVersionScope(ApiVersion.DISTRO_FILTER_MIN_API_VERSION, ApiVersion.DISTRO_FILTER_MAX_API_VERSION),
      DistroFilterDimensionEnum.API_VERSION,
      () => {
        const moduleModel = this.projectModel.getModuleModelByName(distroFilterConfig.moduleName) as CoreModuleModelImpl;
        this.logger
          ._buildError('Invalid apiVersion value for distroFilter.')
          ._detail('Ensure that the value of apiVersion for distroFilter is within the [3, 30] range.')
          ._file(DistroFilterHandler.getDetailFilePath(this.projectModel, moduleModel))
          ._printErrorAndExit();
      },
    ) as number[] | undefined;
  }

  private getApiVersionScope(start: number, end: number): number[] {
    const apiVersionValueList: number[] = [];
    for (let index = start; index <= end; index++) {
      apiVersionValueList.push(index);
    }
    return apiVersionValueList;
  }
}
